<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
    title="Hello Apollo World"
    creationComplete="initApp()" 
    layout="vertical" 
    backgroundColor="#FFFFFF" width="100%" height="100%">
    
    <mx:Script>
        <![CDATA[
        // imports needed for this application
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        import com.pearsoned.apollo.actionscripts.BridgeSound;
        import com.pearsoned.apollo.actionscripts.ConnectionStatus;

        // embed images and declare persistent image classes
        [Embed(source="assets/images/helloapollologo.png")]
        [Bindable] private var imgHelloApollo:Class;
        [Embed(source="assets/images/helloapolloworld1.png")]
        [Bindable] private var imgHelloApolloWorld:Class;
        [Embed(source="assets/images/helloapolloworld2.png")]
        [Bindable] private var imgHelloApolloWorld2:Class;
        [Embed(source="assets/images/helloapolloworld3.png")]
        [Bindable] private var imgHelloApolloWorld3:Class;
        [Embed(source="assets/images/helloapolloworld4.png")]
        [Bindable] private var imgHelloApolloWorld4:Class;
        // declare a persistent Timer (0.2 seconds)
        [Bindable] private var myTimer:Timer = new Timer(200, 0);
        // declare a persistent counter
        [Bindable] private var counter:int = 0;
        private var mySound:BridgeSound = new BridgeSound();
        // declare a one-time use Timer (12 seconds)
        private var myTimer2:Timer = new Timer(12000, 1);
        // declare a connection to test the connection status
        private var connection:ConnectionStatus = new ConnectionStatus();
        // declare a persistent public boolean enabled value for the 'Source' button
        [Bindable] public var blnConnected:Boolean = true;
        // declare a persistent public boolean enabled value for the 'Play Again' button
        [Bindable] public var blnPlayEnabled:Boolean = false;
        
        // initial function called in the ApolloApplication creationComplete method
        private function initApp():void {
            // add an NETWORK_CHANGE event listener: Listens for the connectivity status
            Shell.shell.addEventListener(Event.NETWORK_CHANGE, connection.onConnectionChange);
            // calls the function that checks the connectivity
            connection.checkConnection();
            /* add a timer event listener: Listens for 0.2 second intervals; 
            onTimer() function is called every 0.2 seconds */
            myTimer.addEventListener("timer", onTimer);
            // calls the startHelloTimer() function 
            startHelloTimer();
            // sets the URL for the sound effect
            mySound.url = "http://labs.insideflex.com/apollotraining/apolloworld/assets/sounds/STbridge.MP3";
            // calls the getSound() method that plays the STbridge.MP3
            mySound.getSound();
            /* add a timer event listener: Listens for a 12 second interval; 
            onTimer2() function is called once after 12 seconds */
            myTimer2.addEventListener("timer", onTimer2);
            // start the 12 second timer
            myTimer2.start();
        }
        // soundCompleteHandler() function
        private function soundCompleteHandler(event:Event):void {
            /* set the boolean, blnPlayEnabled, to true;
            this is used to re-enable the 'Play Again' button */
            blnPlayEnabled = true;
        }
        // this function is for re-playing the sound effects
        private function playSoundEffects():void {
            // sets the URL for the sound effect
            mySound.url = "http://labs.insideflex.com/apollotraining/apolloworld/assets/sounds/STbridge.MP3";
            // calls the getSound() method that plays the STbridge.MP3
            mySound.getSound();
            /* add a SOUND_COMPLETE event listener: Listens for the end of the sound effect; 
            soundCompleteHandler() function is called once the listener event occurs */
            mySound.song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
            /* add a timer event listener: Listens for a 12 second interval; 
            onTimer2() function is called once after 12 seconds */
            myTimer2.addEventListener("timer", onTimer2);
            // start the 12 second timer
            myTimer2.start();
        }
        // this function starts the 0.2 second timer
        private function startHelloTimer():void {
            // start the 0.2 second timer
            myTimer.start();
        }
        // this function cycles through the animation images for the revolving earth effect
        private function onTimer(event:TimerEvent):void {
            // conditional logic for rotating through 5 possible images
            switch (counter) {
                case 1:
                    imgApolloLogo.source = imgHelloApolloWorld;
                    break;
                case 2:
                    imgApolloLogo.source = imgHelloApolloWorld2;
                    break;
                case 3:
                    imgApolloLogo.source = imgHelloApolloWorld3;
                    break;
                case 4:
                    imgApolloLogo.source = imgHelloApolloWorld4;
                    counter = 0;
                    break;
                default:
                    imgApolloLogo.source = imgHelloApollo;
                    break;
            }
            // increment counter
            counter++;
        }
        // this function is the Timer2 handler
        private function onTimer2(event:TimerEvent):void {
            // sets the URL for the sound effect
            mySound.url = "http://labs.insideflex.com/apollotraining/apolloworld/assets/sounds/fascinating.MP3";
            // calls the getSound() method that plays the fascinating.MP3
            mySound.getSound();
        }

        private function goThere(sURL:String):void {
            // declare a URLRequest variable that accepts a URL (sURL)
               var u:URLRequest = new URLRequest(sURL);
               // pop a new window using the URL passed in
            navigateToURL(u,"_blank");
        }
        ]]>
    </mx:Script>

    <mx:Style>
        <!-- style for the toolTip displayed when mousing over Flex components -->
        ToolTip { font-family: "Verdanna"; font-size: 14; font-weight: "normal"; 
        background-color: "0x821313"; color: "0xFFFFFF"; }
    </mx:Style>

    <!-- Spacer pushes the content 80 pixels downward -->
    <mx:Spacer height="80"/>
    <!-- Initially displays the image of the apollologo.png and then under AS3 function control, rotates in images -->
    <mx:Image id="imgApolloLogo" source="http://labs.insideflex.com/apollotraining/apolloworld/assets/images/apollologo.png"/>
    <mx:Spacer height="40"/>
    <!-- HBox displays content in a horizontal layout -->
    <mx:HBox>
        <!-- Button controls playing the sound effects -->
        <mx:Button id="btnPlayAgain" label="Play Again" click="playSoundEffects();blnPlayEnabled=false" fontFamily="Verdanna" 
            fontSize="14" fontWeight="bold" toolTip="{(blnPlayEnabled)?'Click to play the sound effects again...':'Button is currently inactive...'}" enabled="{blnPlayEnabled}"/>
        <!-- Button controls opening a new browser window to view the source code -->
        <mx:Button id="btnViewSource" label="Source" click="goThere('http://labs.insideflex.com/apollotraining/apolloworld/bin/srcview/index.html')" fontFamily="Verdanna" 
            fontSize="14" fontWeight="bold" toolTip="{(blnConnected)?'Click to view the source code...':'Offline mode - button is currently inactive...'}"/>
        <!-- Button exits the application -->
        <mx:Button id="btnExit" label="Exit" click="window.close()" fontFamily="Verdanna" 
            fontSize="14" fontWeight="bold" toolTip="Click to exit the application..."/>
    </mx:HBox>
    <mx:Spacer height="40"/>
    <!-- Displays the image that corresponds to connectivity (smile or frown) -->
    <mx:Image id="imgStatus" width="24" height="24"/>    
</mx:ApolloApplication>